package com.icity.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.icity.common.Result;
import com.icity.entity.AllSumary;
import com.icity.entity.Application;
import com.icity.entity.EchartLine;
import com.icity.entity.Echartcolumn;
import com.icity.entity.Echartpie;
import com.icity.entity.EquipmentDistribution;
import com.icity.entity.PartnerMsg;
import com.icity.entity.PermissionsEntity;
import com.icity.entity.RegisteredUse;
import com.icity.entity.ResourceDistribution;
import com.icity.entity.Retentions;
import com.icity.entity.StatisticalSummary;
import com.icity.entity.system.TableSupport;
import com.icity.service.StatisticalSummaryService;
import com.icity.util.PermissionsUtil;

@RestController
@RequestMapping(value = "/admin/statisticalSummary")
public class StatisticalSummaryController {
	@Autowired
	private StatisticalSummaryService statisticalSummaryService;

	/**
	 * 获取首页总记录数(前四个icon)
	 *
	 * @return
	 */
	@RequestMapping(value = "/getCount")
	// @PostMapping(value="/getCount")
	@ResponseBody
	public Result getCount(HttpServletRequest request) {
		Result result = new Result();
		try {
			Map<Object, Object> map = new HashMap<>();
			// 获取主页类型
			String type = request.getParameter("type");
			if (StringUtils.isNotBlank(type)) {
				map.put("type", type);
			} /*
				 * else { result.setCode("-1");
				 *
				 * result.setMessage("参数有误"); }
				 */
			// 获取应用唯一标识
			String application_unique_code = request.getParameter("application_unique_code");
			if (StringUtils.isNotBlank(application_unique_code)) {
				map.put("application_unique_code", application_unique_code);
			} /*
				 * else { result.setCode("-1");
				 *
				 * result.setMessage("参数有误"); }
				 */
			// 获取合伙人id
			String partner_id = request.getParameter("partner_id");
			if (StringUtils.isNotBlank(application_unique_code)) {
				map.put("partner_id", partner_id);
			}

			// List<StatisticalSummary> list =
			// statisticalSummaryService.getAll(map)
			List<StatisticalSummary> list = statisticalSummaryService.getAll(map);
			/*
			 * JSONObject json1 = new JSONObject(); Map<String, Object>
			 * mapsd=new HashMap<>(); mapsd.put("aaa", "aa");
			 *
			 * JSONArray json = new JSONArray(); List<Map<String,Object>> list =
			 * statisticalSummaryService.getAll(map); json.addAll(list);
			 * json1.put("data2", json); json1.put("data1", mapsd);
			 */

			if (list != null && list.size() > 0) {
				result.setMessage("获取成功");
				result.setCode("1");

				result.setData(list);
			} else {
				result.setCode("-1");
				result.setData(list);
				result.setMessage("暂无数据");

			}
		} catch (Exception e) {
			result = new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}

		return result;
	}

	/**
	 * 根据当前登录人获取他的合伙人
	 */
	@RequestMapping(value = "/getPartner")
	// @PostMapping(value="/getCount")
	@ResponseBody
	public Result getPartner(HttpServletRequest request, HttpSession session) {
		Result result = new Result();
		try {
			// 获取当前登录人唯一标识
			/*
			 * String unique_code =request.getParameter("unique_code");
			 * List<PartnerMsg> list =
			 * statisticalSummaryService.getPartner(unique_code);
			 */
			PermissionsEntity permissionsEntity = PermissionsUtil.getPermissions(request);
			List<PartnerMsg> parterner = statisticalSummaryService
					.getPartner(permissionsEntity.getParams().get("partner_id").toString());
			if (parterner != null && parterner.size() > 0) {
				result.setMessage("获取成功");
				result.setCode("1");
				result.setData(parterner);
			} else {
				result.setCode("-1");
				result.setData(new ArrayList<>());
				result.setMessage("暂无数据");
			}
		} catch (Exception e) {
			result = new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}

		return result;
	}

	/**
	 * 根据合伙人查出下方应用
	 */
	@RequestMapping(value = "/getApplication")
	// @PostMapping(value="/getCount")
	@ResponseBody
	public Result getApplication(HttpServletRequest request) {
		Result result = new Result();
		try {

			Map<Object, Object> map = new HashMap<>();
			// 获取主页类型
			Integer type = Integer.valueOf(request.getParameter("type"));
			String unique_code = request.getParameter("unique_code");
			if (type == 1) {// 只有app应用
				// 获取选择的合伙人唯一标识查出下面的app应用

				map.put(type, "1");
				map.put(unique_code, "unique_code");
				List<Application> list = statisticalSummaryService.getApplication(map);

				if (list != null && list.size() > 0) {
					result.setMessage("获取成功");
					result.setCode("1");
					result.setData(list);
				} else {
					result.setCode("-1");
					result.setData(new ArrayList<>());
					result.setMessage("暂无数据");
				}
			} else if (type == 2) {
				// 只有大屏应用
				map.put(type, "4");
				map.put(unique_code, "unique_code");
				List<Application> list = statisticalSummaryService.getApplication(map);
				if (list != null && list.size() > 0) {
					result.setMessage("获取成功");
					result.setCode("1");
					result.setData(list);
				} else {
					result.setCode("-1");
					result.setData(new ArrayList<>());
					result.setMessage("暂无数据");
				}
			} else {
				result.setCode("-2");
				result.setData(new ArrayList<>());
				result.setMessage("未选择主页类型");
			}
		} catch (Exception e) {
			result = new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}

		return result;
	}

	/**
	 * 查询设备位置分布
	 */
	@RequestMapping(value = "/getEquipmentDistribution")
	// @PostMapping(value="/getCount")
	@ResponseBody
	public Result getEquipmentDistribution(HttpServletRequest request) {
		Result result = new Result();
		try {

			Map<Object, Object> map = new HashMap<>();
			// 获取选择的合伙人
			String partner_id = request.getParameter("partner_id");
			if (StringUtils.isNotBlank(partner_id)) {
				map.put("partner_id", partner_id);
			}
			// 获取选择的应用唯一标识
			String application_unique_code = request.getParameter("application_unique_code");
			if (StringUtils.isNotBlank(application_unique_code)) {
				map.put("application_unique_code", application_unique_code);
			}
			String type = request.getParameter("type");
			if (StringUtils.isNotBlank(type)) {
				map.put("type", type);
			}

			if (type.equals("1")) {// app主页

				List<EquipmentDistribution> list = statisticalSummaryService.getEquipmentDistribution(map);
				List<Map<Object, Object>> lists = new ArrayList<>();
				for (EquipmentDistribution equipmentDistribution : list) {
					Map<Object, Object> maps = new HashMap<>();
					maps.put("name", equipmentDistribution.getProvince());
					maps.put("value", equipmentDistribution.getLocation_num());
					lists.add(maps);
				}

				if (lists != null && lists.size() > 0) {
					result.setMessage("获取成功");
					result.setCode("1");
					result.setData(lists);
				} else {
					result.setCode("-1");
					result.setData(new ArrayList<>());
					result.setMessage("暂无数据");
				}
			} else if (type.equals("2")) {// 大屏主页
				List<EquipmentDistribution> list = statisticalSummaryService.getEquipmentDistribution(map);
				List<Map<Object, Object>> lists = null;
				for (EquipmentDistribution equipmentDistribution : list) {
					Map<Object, Object> maps = new HashMap<>();
					maps.put("name", equipmentDistribution.getProvince());
					maps.put("value", equipmentDistribution.getLocation_num());
					lists.add(maps);
				}
				if (lists != null && lists.size() > 0) {
					result.setMessage("获取成功");
					result.setCode("1");
					result.setData(lists);
				} else {
					result.setCode("-1");
					result.setData(new ArrayList<>());
					result.setMessage("暂无数据");
				}

			}

		} catch (Exception e) {
			result = new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}

		return result;
	}

	/**
	 * 查询设备省位置分布
	 */
	@RequestMapping(value = "/getEquipmentProvinceNUmDistribution")
	// @PostMapping(value="/getCount")
	@ResponseBody
	public Result getEquipmentProvinceDistribution(HttpServletRequest request) {
		Result result = new Result();
		try {

			Map<Object, Object> map = new HashMap<>();
			// 获取选择的合伙人
			String partner_id = request.getParameter("partner_id");
			if (StringUtils.isNotBlank(partner_id)) {
				map.put("partner_id", partner_id);
			}
			// 获取选择的应用唯一标识
			String application_unique_code = request.getParameter("application_unique_code");
			if (StringUtils.isNotBlank(application_unique_code)) {
				map.put("application_unique_code", application_unique_code);
			}
			String type = request.getParameter("type");
			if (StringUtils.isNotBlank(type)) {
				map.put("type", type);
			}

			if (type.equals("1")) {// app主页
				List<Object> liste = new ArrayList<>();
				// 查询出省份
				List<String> list = statisticalSummaryService.getEquipmentProvinceDistribution(map);
				// 查询出省份数量
				List<Integer> lists = statisticalSummaryService.getEquipmentProvinceNumDistribution(map);
				liste.add(list);
				liste.add(lists);
				if (liste != null && liste.size() > 0) {
					result.setMessage("获取成功");
					result.setCode("1");
					result.setData(liste);
				} else {
					result.setCode("-1");
					result.setData(new ArrayList<>());
					result.setMessage("暂无数据");
				}
			} else if (type.equals("2")) {// 大屏主页
				List<String> list = statisticalSummaryService.getEquipmentProvinceDistribution(map);
				// 查询出省份数量
				List<Integer> lists = statisticalSummaryService.getEquipmentProvinceNumDistribution(map);

				if (list != null && list.size() > 0) {
					result.setMessage("获取成功");
					result.setCode("1");
					result.setData(list);
				} else {
					result.setCode("-1");
					result.setData(new ArrayList<>());
					result.setMessage("暂无数据");
				}

			}

		} catch (Exception e) {
			result = new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}

		return result;
	}

	/**
	 * 查询设备市位置分布
	 */
	@RequestMapping(value = "/getEquipmentCityNUmDistribution")
	// @PostMapping(value="/getCount")
	@ResponseBody
	public Result getEquipmentcityDistribution(HttpServletRequest request) {
		Result result = new Result();
		try {

			Map<Object, Object> map = new HashMap<>();
			// 获取选择的合伙人
			String partner_id = request.getParameter("partner_id");
			if (StringUtils.isNotBlank(partner_id)) {
				map.put("partner_id", partner_id);
			}
			// 获取选择的应用唯一标识
			String application_unique_code = request.getParameter("application_unique_code");
			if (StringUtils.isNotBlank(application_unique_code)) {
				map.put("application_unique_code", application_unique_code);
			}
			String type = request.getParameter("type");
			if (StringUtils.isNotBlank(type)) {
				map.put("type", type);
			}

			if (type.equals("1")) {// app主页
				List<Object> liste = new ArrayList<>();
				// 查询出省份
				List<String> list = statisticalSummaryService.getEquipmentcityDistribution(map);
				// 查询出省份数量
				List<Integer> lists = statisticalSummaryService.getEquipmentProvinceNumDistribution(map);
				liste.add(list);
				liste.add(lists);
				if (liste != null && liste.size() > 0) {
					result.setMessage("获取成功");
					result.setCode("1");
					result.setData(liste);
				} else {
					result.setCode("-1");
					result.setData(new ArrayList<>());
					result.setMessage("暂无数据");
				}
			} else if (type.equals("2")) {// 大屏主页
				List<String> list = statisticalSummaryService.getEquipmentcityDistribution(map);
				// 查询出省份数量
				List<Integer> lists = statisticalSummaryService.getEquipmentProvinceNumDistribution(map);

				if (list != null && list.size() > 0) {
					result.setMessage("获取成功");
					result.setCode("1");
					result.setData(list);
				} else {
					result.setCode("-1");
					result.setData(new ArrayList<>());
					result.setMessage("暂无数据");
				}

			}

		} catch (Exception e) {
			result = new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}

		return result;
	}

	/**
	 * 获取app应用注册用户数量
	 */
	@RequestMapping(value = "/getAppRegistered")
	// @PostMapping(value="/getCount")
	@ResponseBody
	public Result getAppRegistered(HttpServletRequest request) {
		Result result = new Result();
		try {
			// 获取选择的合伙人
			String partner_id = request.getParameter("partner_id");
			// 获取选择的应用唯一标识
			String application_unique_code = request.getParameter("application_unique_code");
			Map<Object, Object> map = new HashMap<>();
			map.put(partner_id, "partner_id");
			map.put(application_unique_code, "application_unique_code");
			List<RegisteredUse> list = statisticalSummaryService.getAppRegisteredCount(map);
			if (list != null && list.size() > 0) {
				result.setMessage("获取成功");
				result.setCode("1");
				result.setData(list);
			} else {
				result.setCode("-1");
				result.setData(new ArrayList<>());
				result.setMessage("暂无数据");
			}
		} catch (Exception e) {
			result = new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}

		return result;
	}

	/**
	 * 获取资源分布数量 (只有大屏应用)
	 */
	@RequestMapping(value = "/getResourcesDistribution")

	@ResponseBody
	public Result getResourcesDistribution(HttpServletRequest request) {
		Result result = new Result();
		try {
			Map<Object, Object> map = new HashMap<>();
			// 获取选择的合伙人
			String partner_id = request.getParameter("partner_id");
			if (StringUtils.isNotBlank(partner_id)) {
				map.put("partner_id", partner_id);
			}
			// 获取选择的应用唯一标识
			String application_unique_code = request.getParameter("application_unique_code");
			if (StringUtils.isNotBlank(application_unique_code)) {
				map.put("application_unique_code", application_unique_code);
			}

			Echartpie echartLine = new Echartpie();
			Map<Object, String> tooltip = statisticalSummaryService.getTooltip();
			echartLine.setTooltip(tooltip);

			Map<Object, Object> legend = statisticalSummaryService.getLegend();
			echartLine.setLegend(legend);

			List<Map<Object, Object>> listSeries = statisticalSummaryService.getSeries(map);
			echartLine.setSeries(listSeries);

			result.setData(echartLine);
			result.setCode("1");
			result.setMessage("获取数据成功");
			return result;
		} catch (Exception e) {
			result = new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}

		return result;
	}

	/**
	 * 获取资源分布数量 (只有大屏应用)
	 */
	@RequestMapping(value = "/getResourcesDistribution1")

	@ResponseBody
	public Result getResourcesDistribution1(HttpServletRequest request) {
		Result result = new Result();
		try {
			Map<Object, Object> map = new HashMap<>();
			// 获取选择的合伙人
			String partner_id = request.getParameter("partner_id");
			if (StringUtils.isNotBlank(partner_id)) {
				map.put("partner_id", partner_id);
			}
			// 获取选择的应用唯一标识
			String application_unique_code = request.getParameter("application_unique_code");
			if (StringUtils.isNotBlank(application_unique_code)) {
				map.put("application_unique_code", application_unique_code);
			}

			ResourceDistribution list = statisticalSummaryService.getResourceDistribution1(map);
			List<Map<Object, Object>> data = new ArrayList<>();
			Map<Object, Object> maps = new HashMap<>();
			maps.put("value", list.getResources_package_audio_num());
			maps.put("name", "视频");
			Map<Object, Object> mapsd = new HashMap<>();
			mapsd.put("value", list.getResources_package_book_num());
			mapsd.put("name", "书籍");
			Map<Object, Object> mapsf = new HashMap<>();
			mapsf.put("value", list.getResources_package_journal_num());
			mapsf.put("name", "期刊");
			Map<Object, Object> mapsh = new HashMap<>();
			mapsh.put("value", list.getResources_package_news_num());
			mapsh.put("name", "报纸");
			data.add(maps);
			data.add(mapsd);
			data.add(mapsf);
			data.add(mapsh);

			result.setData(data);
			result.setCode("1");
			result.setMessage("获取数据成功");
			return result;
		} catch (Exception e) {
			result = new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}

		return result;
	}

	/**
	 * 系统访问量每周概括
	 */
	@RequestMapping(value = "/getSystemTraffic")

	@ResponseBody
	public Result getSystemTraffic(HttpServletRequest request) {
		Result result = new Result();
		try {

			Map<Object, Object> map = new HashMap<>();
			// 获取选择的合伙人
			String partner_id = request.getParameter("partner_id");
			if (StringUtils.isNotBlank(partner_id)) {
				map.put("partner_id", partner_id);
			}
			// 获取选择的应用唯一标识
			String application_unique_code = request.getParameter("application_unique_code");
			if (StringUtils.isNotBlank(application_unique_code)) {
				map.put("application_unique_code", application_unique_code);
			}
			String type = request.getParameter("type");
			if (StringUtils.isNotBlank(type)) {
				map.put("type", type);
			}

			if (type.equals("1")) {// app主页
				// List<Object> liste=new ArrayList<>();

				// List<String>
				// list=statisticalSummaryService.getEquipmentcityDistribution(map);
				// 查询出省份数量
				List<Integer> lists = statisticalSummaryService.getSystemTraffic(map);
				// liste.add(list);
				// liste.add(lists);
				if (lists != null && lists.size() > 0) {
					result.setMessage("获取成功");
					result.setCode("1");
					result.setData(lists);
				} else {
					result.setCode("-1");
					result.setData(new ArrayList<>());
					result.setMessage("暂无数据");
				}
			} else if (type.equals("2")) {// 大屏主页
				// List<String>
				// list=statisticalSummaryService.getEquipmentcityDistribution(map);
				// 查询出省份数量
				List<Integer> lists = statisticalSummaryService.getSystemTraffic(map);

				if (lists != null && lists.size() > 0) {
					result.setMessage("获取成功");
					result.setCode("1");
					result.setData(lists);
				} else {
					result.setCode("-1");
					result.setData(new ArrayList<>());
					result.setMessage("暂无数据");
				}

			}

		} catch (Exception e) {
			result = new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}

		return result;
	}

	@RequestMapping(value = "/allnum")
	@ResponseBody
	public Result getArticleList(HttpServletRequest request) {
		Result result = new Result();
		try {
			AllSumary allSumary = statisticalSummaryService.getAllSumary();
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(allSumary);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

}
